package com.codeiy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codeiy.criteria.CriteriaQuery;
import com.codeiy.dto.R;
import com.codeiy.entity.meta.MetaField;
import com.codeiy.service.MetaFieldService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/field")
public class MetaFieldController {
    private final MetaFieldService metaFieldService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return MetaField
     */
    @GetMapping("/{id:\\d+}")
    public R<MetaField> getById(@PathVariable Long id) {
        return R.ok(metaFieldService.getById(id));
    }

    /**
     * 查询分页信息
     *
     * @param criteriaQuery 查询对象
     * @return 分页对象
     */
    @PostMapping("/page")
    public R<IPage<MetaField>> page(@RequestBody CriteriaQuery criteriaQuery) {
        return R.ok(metaFieldService.page(criteriaQuery.toMybatisPlusPage(), criteriaQuery.toQueryWrapper()));
    }

    /**
     * 添加
     *
     * @param metaField 实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> save(@RequestBody MetaField metaField) {
        return R.ok(metaFieldService.save(metaField));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id:\\d+}")
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(metaFieldService.removeById(id));
    }

    /**
     * 编辑
     *
     * @param metaField 实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> update(@RequestBody MetaField metaField) {
        return R.ok(metaFieldService.updateById(metaField));
    }

    /**
     * 根据列编码查询列信息
     *
     * @param fieldCode 列编码
     * @return
     */
    @GetMapping("/details/{fieldCode}")
    public R<MetaField> user(@PathVariable String fieldCode) {
        MetaField condition = new MetaField();
        condition.setFieldCode(fieldCode);
        return R.ok(metaFieldService.getOne(new QueryWrapper<>(condition)));
    }
}
